package com.china.lottery.listener.task.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.china.lottery.listener.bean.Banlance;
import com.china.lottery.listener.dao.BetDao;
import com.china.lottery.listener.dao.DiaryInfoDao;
import com.china.lottery.listener.dao.DictionaryDao;
import com.china.lottery.listener.dao.entity.LBet;
import com.china.lottery.listener.dao.entity.LChannelInfo;
import com.china.lottery.listener.task.core.BaseNoteService;
import com.china.lottery.listener.util.Constant;
import com.china.lottery.listener.util.HttpClient;
import com.china.lottery.listener.util.HttpClient3;
import com.china.lottery.listener.util.MD5;
import com.china.lottery.listener.util.Tools;

/**
 * 轮机号余额预警Service
 * @author tuquan
 * @date 2017年4月7日
 */
@Component("channelBanlanceWarnService")
public class ChannelBanlanceWarnService extends BaseNoteService {

	private static String NUMCODE = Constant.CHANNELBANLANCE;
	private static String CHANNELURL = Constant.URL;

	@Autowired
	private DictionaryDao dicDao;

	@Autowired
	private DiaryInfoDao diaryInfoDao;

	@Override
	public List<String> getNumbers() {

		return this.getNumberFromParent(dicDao.findByParentCode(NUMCODE));
	}
	
	String url = "http://150.242.239.203:28047/ChinaLotteryPlatform/lotteryAgent!";

	@Autowired
	private BetDao betDao;
	
	
	@Override
	public String getContent() {
		
		Date now = new Date();// 当前时间
		Date per2date = Tools.getPreMinutes(now, 715);// 获取前2天的时间
		Date per30min = Tools.getPreMinutes(now, 1);// 获取   //Sun May 27 23:57:03 CST 2018==========Mon May 28 09:56:03 CST 2018
		List<LBet> li = betDao.queryLBetById(per2date, per30min,LBet.TICKETSTATUS_SUCC, null);
		HttpClient3 http = new HttpClient3();
		for (int i = 0; i < li.size(); i++) {
			long startTime = System.currentTimeMillis();
			Map<String, String> params = new HashMap<String, String>();  
		    params.put("merid", "29002000104");
	  		
	  		params.put("agentOrderId", "HX-TDGY" + String.valueOf((new java.util.Date()).getTime())+1);
	  		params.put("cardType", "1");
	  		params.put("cardNumber", "410042199901011234");
	  		params.put("name", "张三");
	  		params.put("mobile", "15021987892");
	  		params.put("isAdd", "0");
	  		params.put("msg", "测试");
	  		params.put("timestamp", Tools.getTimestamp());  		
			
			params.put("service", "lotteryBet");
	  		params.put("lotteryCode", "42");  //42 足球 ，43 篮球
	  		params.put("periodCode", "201805303001");
	  		
	  		params.put("castcode",li.get(i).getBetCode());
	  		
	  		params.put("count", "1"); //总倍数写死
			params.put("amount", String.valueOf((new Double(li.get(i).getMoney())).intValue()));//总金额 为 castcode 金额的总和
			 
			params.put("multiple", "1"); //倍数为1写死		 
			
			params.put("playType", li.get(i).getPlayType()); // 竞彩渠道玩法 
			params.put("castmethod", String.valueOf(li.get(i).getCastMethod()));   //	投注方式对应渠道玩法 
			//足球6种 : 胜平负(20201),比分(20202),总进球(20203),半场胜平负(20204),混合串关玩法(20205),让分胜平负(20206) 
			//篮球 5种: 胜负(20401),让分胜负(20402),胜分差(20403),大小分(20404),混合串关玩法(20405)
			
			//足球6种 : 1,2,3,4,5,6
			//篮球 5种: 1,2,3,4,5
			
			params.put("playmethod", "1");   //玩法 为 1写死
			
//			System.out.println(params.get("agentOrderId"));
			
			params.put("digest", getKeyString(params));
			String aaa = dopost(url+ params.get("service"), params,http);
			log.info("do post lotteryBet response:"+aaa);
			if(aaa.equals("9999")){
				System.out.println("======="+i+"=========:"+aaa);
			}
			long endTime = System.currentTimeMillis();
			System.out.println("======"+i+"=======: "+(endTime - startTime)+"ms");
		}
		return null;
	}
	
	private String getKeyString(Map<String, String> params) {
		StringBuffer sb = new StringBuffer();
		sb.append("AUTOLOTTERY");
		sb.append(params.get("service"));
		sb.append(params.get("timestamp"));
		sb.append("AUTOLOTTERY");
		
		String sign = MD5.encodeString(sb.toString(), "UTF-8");
		return sign;
	}
	
	private String dopost(String url, Map<String, String> params,HttpClient3 http) {
		String message = null;
		try {
			message = http.doPost(url, params);
		} catch (Exception e) {
			log.error("do post lotteryBet error:{}", e.getMessage());
		}
	return message;
}
	
//	@Override
//	public String getContent() {
//		boolean flag = false;
//		String message = "";
//		StringBuffer sb = new StringBuffer("");
//		String result = getChannelMoeny("100006");
//		String[] arrayBanlance = result.split(";");
//		for (int i = 0; i < arrayBanlance.length; i++) {
//			String logicCode = arrayBanlance[i].split(":")[0].toString();
//			double banlance = Double.valueOf(arrayBanlance[i].split(":")[1].toString());
//			if (banlance<Constant.CHANNEL_WARN_MONEY) {
//				sb.append(logicCode+":"+banlance);
//				flag = true;
//			}
//		}
//		if (flag) {
//			message = "江西渠道轮机号预警:"+sb.toString()+",以上轮机号不足"+Constant.CHANNEL_WARN_MONEY+"元,请及时充值";
//		}
//		return message;
//	}

	public String channelQuery() {
		List<LChannelInfo> channel = diaryInfoDao.findAllChannel();
		for (LChannelInfo channelarr : channel) {
			getChannelMoeny(channelarr.getChannelCode());

		}
		return null;
	}

	public String getChannelMoeny(String channelCode) {

		Map<String, String> params = new HashMap<String, String>();
		params.put("channelCode", channelCode);
		String result = null;
		try {
			log.info("query channel money: url:{},params:{}", CHANNELURL,
					params);
			result = HttpClient.getUrlAsString(CHANNELURL, params);
			if (!Tools.isBlank(result)) {

				JSONObject json = JSONObject.fromObject(result);
				Banlance b = (Banlance) JSONObject.toBean(json, Banlance.class);
				if (Tools.isBlank(b.getBalance())) {
					result = "0";
				} else {
					result = String.valueOf(b.getBalance());
				}
			}
		} catch (Exception e) {
			log.info(e.getMessage(), e);

		}

		return result;

	}
}
